$("#form-member").validate({
    rules: {
        category_id: {
            maxlength:10,
            minlength:10,
        },
        logmax: {
            maxlength:30,
        },
        logmin: {
            maxlength:10,
            minlength:10,
        },
        shaixuan: {
            maxlength:10,
            minlength:10,
        }

    },
    //提交表单数据
    submitHandler:function(form){
        //获取
        var category_id=$("#category_id").val(),
            logmax=$("#logmax").val(),
            logmin=$("#logmin").val(),
            shaixuan=$("#shaixuan").val(),
            text=$("#text").val()
        ;
        console.log(category_id)
        //提交
        var user_data = {
            category_id:category_id,
            created_at_end:logmax,
            created_at_start:logmin,
            status:shaixuan,
            title:text
        }
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataSource = new Array();
                var table;   //table对象
                dataSource = [];
                var result=data.data
                $.each(result, function (i, field){
                    var rowData = new Array();
                    //活动标题-0
                    rowData.push(field.title);
                    //优惠券主题-1
                    rowData.push(field.coupon_name);
                    //有效时间-2
                    rowData.push(field.started_at + " - " + field.end_in);
                    //发放时间-3
                    rowData.push(field.issue_started_at + " - " + field.issue_end_in);
                    //发放数量-4
                    rowData.push(field.issue_count);
                    //状态-5
                    rowData.push(field.status);
                    //创家人-6
                    rowData.push(field.created_by);
                    //创建时间-7
                    rowData.push(field.created_at);
                    // ID-8
                    rowData.push(field.promotion_id);
                    // 优惠券ID-9
                    rowData.push(field.coupon_id);

                    dataSource.push(rowData);
                });
                initDataTable(dataSource);
                return false
            }else{
                //报错
                console.log("123")
                var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
                dttable.fnClearTable(); //清空一下table
                return false
            }
        };
        //给一个接口地址提交数据
        return web.doAjax("http://192.168.8.249:8088/operation/search_promotion_conf.jhtml", 'post', user_data, succCallBack);
        //获取数据之后如何展现在页面上
        function initDataTable(dataSource) {
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable
            $("#datatable").DataTable({
                "autoWidth": false,
                "ordering": true,
                "searching": true,
                "paging": true,
                "retrieve": true,
                "destroy": true,
                "bAutoWidth": true,
                "bLengthChange": true,
                "aLengthMenu":[5,10,25,50],
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    },
                    "sZeroRecords": "没有检索到数据",
                    "sProcessing": "<img src='./loading.gif' />"
                },
                data: dataSource,
                columnDefs: [
                    {
                        "visible": true,
                        "targets": 0
                    },
                    {
                        "render": function(data,type,row,meta){
                            var val ='<a><i class="Hui-iconfont mr-10" onclick="edit_data('+row[8]+',\''+row[0]+'\','+row[9]+',\''+row[3]+'\','+row[4]+')">&#xe6df;</i></a> <a><i class="Hui-iconfont" onclick="delete_data('+row[8]+')">&#xe6e2;</i></a>';
                            return val;
                        },
                        "targets": 8
                    },
                ]
            });
        };

    }
})
function delete_data(id){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: "http://192.168.8.249:8088/operation/delete_promotion_conf.jhtml",
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                promotion_id:id,
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }

        })

    });

}
function edit_data(promotion_id,title,coupon_id,issue_time,issue_count){
    var time = issue_time.split(" - ");
    var index = layer.open({
        type: 2,
        title:"编辑",
        content: "promotion_edit.html"+"?promotion_id="+promotion_id+"&title="+title+"&coupon_id="+coupon_id+"&issue_started_at="+time[0]+"&issue_end_in="+time[1]+"&issue_count="+issue_count
    });
    layer.full(index);
}
/*
下拉框的展示
 */
$(function () {
    var category_id=$("#category_id").val();
    $.ajax({
        type: "post",
        url: "http://192.168.8.249:8088/operation/get_promotion_category.jhtml",
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:category_id,type:102
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
            }
        }
    })
})